﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.StaffPages
{
    /// <summary>
    /// ManageStaff 的摘要说明。
    /// </summary>
    public partial class ManageStaffPage : Page
    {
        protected bool ShowNormalStaffs
        {
            get;
            private set;
        }

        private string _userName;

        protected override void OnInit(EventArgs e)
        {
            ShowNormalStaffs = !QSUtil.GetBoolean("DisplayType", false);
            _userName = SiteContext.Current.UserName;

            cmdDimission.Click += cmdDimission_Click;
            cmdRehab.Click += cmdRehab_Click;
            cmdChangePosition.Click += cmdChangePosition_Click;

            cmdRehab.OnClientClick = "if(true !== window.confirm(\"是否复职？\")){return false;}";
            cmdDimission.OnClientClick = "if(true !== window.confirm(\"是否离职？\")){return false;}";
            cmdChangePosition.OnClientClick = "if(true !== window.confirm(\"是否调职？\")){return false;}";

            if (ShowNormalStaffs)
            {
                cmdRehab.Visible = false;
                cmdDimission.Visible = true;
                cmdChangePosition.Visible = true;
            }
            else
            {
                cmdRehab.Visible = true;
                cmdDimission.Visible = false;
                cmdChangePosition.Visible = false;
            }
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }

            base.OnLoad(e);
        }

        private void BindGrid()
        {
            dbStaffList.DataSource = Staff.GetAllStaff(!ShowNormalStaffs);
            dbStaffList.DataBind();
        }

        //离职,复职通知
        private void sms_all(int i)
        {
            var list = Tools.FindSelectedItems(dbStaffList, "chkStaff_ID");

            // 处理短信提醒
            var selectedStaffs = Staff.GetStaffInfo(list);
            if (selectedStaffs.Count > 0)
            {
                var staffList = Staff.GetAllStaff(false);

                foreach (var selectedStaff in selectedStaffs)
                {
                    var sm = new SMS();
                    string Position_name = selectedStaff.PositionName;
                    foreach (var staff in staffList)
                    {
                        if (i == 0)
                        {
                            SMS.SendLocalMsg(_userName, new string[] { staff.Name }, Position_name + " 处员工:" + selectedStaff.RealName + ",已经离职,特此通知.");
                        }
                        else
                        {
                            SMS.SendLocalMsg(_userName, new string[] { staff.Name }, Position_name + " 处员工:" + selectedStaff.RealName + ",已经恢复原职,特此通知.");
                        }
                    }
                }
            }
        }

        private void cmdDimission_Click(object sender, EventArgs e)
        {
            var list = Tools.FindSelectedItems(dbStaffList, "chkStaff_ID");
            if (list.Count > 0)
            {
                if (Staff.Dimission(list))
                {
                    if (cbRemind.Checked)
                    {
                        sms_all(0);
                    }

                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要离职的人员！');</script>");
            }
        }

        private void cmdRehab_Click(object sender, EventArgs e)
        {
            var list = Tools.FindSelectedItems(dbStaffList, "chkStaff_ID");
            if (list.Count > 0)
            {
                if (Staff.Rehab(list))
                {
                    if (cbRemind.Checked)
                    {
                        sms_all(1);
                    }
                    //Response.Write("<script language=javascript>alert('复职成功！');</script>");		
                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要复职的人员！');</script>");
            }
        }

        private void cmdChangePosition_Click(object sender, EventArgs e)
        {
            var list = Tools.FindSelectedItems(dbStaffList, "chkStaff_ID");
            if (list.Count > 0)
            {
                var intArray = new int[list.Count];
                list.CopyTo(intArray, 0);
                var stringArray = Array.ConvertAll(intArray, src => src.ToString());
                var strStaffID = string.Join(",", stringArray);
                // string[] array = Array.ConvertAll(list.
                Response.Redirect("../Position/ChangePosition.aspx?StaffIDS=" + strStaffID + "&ReturnPage=1");
            }
            else
            {
                //Response.Write("<script language=javascript>alert('请选择要调职的人员！');</script>");		
                // Page.RegisterStartupScript("window", "<script language=javascript>alert('请选择要调职的人员！');</script>");
                ClientScript.RegisterStartupScript(GetType(), "window", "<script language=javascript>alert('请选择要调职的人员！');</script>");
            }
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}